const App = getApp();

// 工具类
import Util from '../../../utils/util.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    teamId: 0,
    detail: [],
    nid:0,
    peopleNum:1,
    showPopup:false,
    nowDate:Util.dateFormat('YYYY-mm-dd HH:MM', new Date()),
    theme:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      nid:options.nid
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let _this = this;
    _this.setData({
      isLogin: App.checkIsLogin(),
    });
    if (_this.data.isLogin) {
      _this.setData({
        teamId: App.checkTeam(),
      });

      // 详细信息
      _this.getDetail();
    }
  },

  /**
   * 详细信息
   */
  getDetail() {
    let _this = this;
    App._get('activity/detail', {activity_id:_this.data.nid}, result => {
      let themeValue=result.data.detail.theme_label
      let theme = [];
      if(themeValue){
        theme=themeValue.split(",")
      }
      _this.setData({
        detail: result.data.detail,
        theme
      })
    });
  },
  

  // 报名弹窗
  onShowPopup(e){
    let that = this;
    that.setData({
      showPopup: !that.data.showPopup
    }); 
  },
  //提交
  formSubmit(e) {
    let formD = e.detail.value;//获取表单数据
    let formStatus = false
    for (let key in formD) {
      if(formD[key] == ""){
        formStatus = true
        break;
      }
    }
    if(formStatus){
      App.showError("姓名和电话不能空！");
      return;
    }
    let obj ={
      name:formD.userName,
      phone:formD.userTel,
      people_num:this.data.peopleNum|1,
      activity_id:this.data.activity_id
    }
    console.log(obj);
    let that =this
    App._post_form('activity/signup', obj, result => {
      //成功提示
      App.showSuccess(result.msg, function() {
        that.setData({
          showPopup: false
        }); 
        that.getDetail()
      });

    });
  },
   /**
   * 自定义输入数量
   */
  onInputNum(e) {
    let _this = this,
      iptValue = e.detail.value;
    if (!Util.isPositiveInteger(iptValue) && iptValue !== '') {
      iptValue = 1;
    }
    _this.setData({
      peopleNum: iptValue
    });
    console.log( _this.data.peopleNum)
  },
  /**
   * 增加数量
   */
  onIncNumber(e) {
    let _this = this;
    App.saveFormId(e.detail.formId);
    _this.setData({
      peopleNum: ++_this.data.peopleNum
    })
  },

  /**
   * 减少数量
   */
  onDecNumber(e) {
    let _this = this;
    App.saveFormId(e.detail.formId);
    if (_this.data.peopleNum > 1) {
      _this.setData({
        peopleNum: --_this.data.peopleNum
      });
    }
  },
  /**
  * 分享当前页面
  */
  onShareAppMessage() {
    return {
      title: this.data.detail.activity_title,
      path: "pages/activity/detail/index?nid="+this.data.nid
    };
  },
  /**
   * 分享到朋友圈
   */
  onShareTimeline() {
    return {
      title: this.data.detail.activity_title,
      path: "pages/activity/detail/index?nid="+this.data.nid
    };
  },

})